package com.bt.mes.brm.provider.service;

import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_ADD;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_CANCEL;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_DELETE;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_QUERY;
import static com.bt.mes.common.bean.def.GenericDef.ACTION_FLG_UPDATE;
import static com.bt.mes.common.bean.def.GenericDef.E_XPBISQRS_MDL_NOT_FOUND;
import static com.bt.mes.common.bean.def.GenericDef.E_XPBISQRS_NOT_FOUND_QRS;
import static com.bt.mes.common.bean.def.GenericDef.E_XPBISQRS_QRS_ALREADY_EXIST;
import static com.bt.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.bt.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.bt.mes.brm.api.service.IXpbisqrsService;
import com.bt.mes.brm.provider.tx.Xpbisqrs.XpbisqrsI;
import com.bt.mes.brm.provider.tx.Xpbisqrs.XpbisqrsIA;
import com.bt.mes.brm.provider.tx.Xpbisqrs.XpbisqrsO;
import com.bt.mes.brm.provider.tx.Xpbisqrs.XpbisqrsOA;
import com.bt.mes.common.dao.IBisQrsDao;
import com.bt.mes.common.util.MqOpe;
import com.bt.table.ppt.Bis_qrs;
import com.bt.table.ppt.Bis_qrsId;

@Service("xpbisqrsService")
public class XpbisqrsService implements IXpbisqrsService {
	private Logger logger = Logger.getLogger(XpbisqrsService.class);
	@Autowired
	private XpbisqrsI inTrx;

	@Autowired
	private XpbisqrsO outTrx;

	@Autowired
	private IBisQrsDao bisQrsDao;

	@Autowired
	private MqOpe mqOpe;

	public String subMainProc(String strInMsg) {
		logger.info("InTrx is " + strInMsg);
		try {
			outTrx.setTrx_id("XPBISQRS");
			outTrx.setType_id("O");
			inTrx = (XpbisqrsI) mqOpe.inStr2Obj(strInMsg, inTrx);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch (action_flg) {
			case ACTION_FLG_QUERY:
				queryByConditionFnc();
				break;
			case 'L':
				queryQrsKeyListFnc();
				break;
			case ACTION_FLG_ADD:
				addFnc();
				break;
			case ACTION_FLG_UPDATE:
				updateFnc();
				break;
			case ACTION_FLG_DELETE:
				deleteFnc();
				break;
			case ACTION_FLG_CANCEL:
				deleteByConditionFnc();
				break;
			default:
				errActionFnc();

			}
			Session session = bisQrsDao.getCrSession();
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			outTrx.setTrx_id("XPBISQRS");
			outTrx.setType_id("O");
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.getCause().toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			logger.info(mqOpe.obj2Str(outTrx));
		}
		return mqOpe.obj2Str(outTrx);

	}

	public void addFnc() {

		XpbisqrsIA iary = inTrx.getIary().get(0);
		Bis_qrsId id = new Bis_qrsId();
		id.setMdl_id_fk(iary.getMdl_id_fk());
		String mdl_typ_fk = getMdlTypByMdlIDProc(iary.getMdl_id_fk());
		if (mdl_typ_fk == null) {
			outTrx.setRtn_code(E_XPBISQRS_MDL_NOT_FOUND + "");
			outTrx.setRtn_mesg("not found mdl_id[" + iary.getMdl_id_fk() + "]");
			return;
		}
		id.setMdl_typ_fk(mdl_typ_fk);
		id.setOpe_id_fk(iary.getOpe_id_fk());
		id.setOpe_no_fk(iary.getOpe_no_fk());
		id.setOpe_ver_fk(iary.getOpe_ver_fk());
		id.setPath_id_fk(iary.getPath_id_fk());
		id.setPath_ver_fk(iary.getPath_ver_fk());
		id.setQrs_id(iary.getQrs_id());
		id.setTool_id_fk(iary.getTool_id_fk());
		Bis_qrs queryBisQrs = bisQrsDao.get(id);
		if (queryBisQrs != null) {
			outTrx.setRtn_code(E_XPBISQRS_QRS_ALREADY_EXIST + "");
			outTrx.setRtn_mesg("Record already Exist");
			return;
		}
		Bis_qrs bis_qrs = new Bis_qrs();
		bis_qrs.setId(id);
		bis_qrs.setQrk_time(iary.getQrk_time());
		bis_qrs.setQrs_ope_id_fk(iary.getQrs_ope_id_fk());
		bis_qrs.setQrs_ope_no_fk(iary.getQrs_ope_no_fk());
		bis_qrs.setQrs_ope_ver_fk(iary.getQrs_ope_ver_fk());
		bis_qrs.setQrs_path_id_fk(iary.getQrs_path_id_fk());
		bis_qrs.setQrs_path_ver_fk(iary.getQrs_path_ver_fk());
		bis_qrs.setQrs_time(iary.getQrs_time());
		bis_qrs.setQrs_typ(iary.getQrs_typ());
		bis_qrs.setSwh_path_id_fk(iary.getSwh_path_id_fk());
		bis_qrs.setSwh_path_ver_fk(iary.getSwh_path_ver_fk());

		bisQrsDao.save(bis_qrs);

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
	}

	public XpbisqrsO updateFnc() {
		XpbisqrsIA iary = inTrx.getIary().get(0);
		Bis_qrsId id = new Bis_qrsId();
		id.setMdl_id_fk(iary.getMdl_id_fk());
		String mdl_typ_fk = getMdlTypByMdlIDProc(iary.getMdl_id_fk());
		if (mdl_typ_fk == null) {
			outTrx.setRtn_code(E_XPBISQRS_MDL_NOT_FOUND + "");
			outTrx.setRtn_mesg("not found mdl_id[" + iary.getMdl_id_fk() + "]");
			return outTrx;
		}
		id.setMdl_typ_fk(mdl_typ_fk);
		id.setOpe_id_fk(iary.getOpe_id_fk());
		id.setOpe_no_fk(iary.getOpe_no_fk());
		id.setOpe_ver_fk(iary.getOpe_ver_fk());
		id.setPath_id_fk(iary.getPath_id_fk());
		id.setPath_ver_fk(iary.getPath_ver_fk());
		id.setQrs_id(iary.getQrs_id());
		id.setTool_id_fk(iary.getTool_id_fk());
		Bis_qrs bis_qrs = bisQrsDao.get(id);
		if (bis_qrs == null) {
			outTrx.setRtn_code(E_XPBISQRS_NOT_FOUND_QRS + "");
			outTrx.setRtn_mesg("not Found Record[BIS_MDL_DEF]");
			return outTrx;
		}
		if (iary.getQrk_time() != null) {
			bis_qrs.setQrk_time(iary.getQrk_time());
		}
		if (iary.getQrs_ope_id_fk() != null) {
			bis_qrs.setQrs_ope_id_fk(iary.getQrs_ope_id_fk());
		}
		if (iary.getQrs_ope_no_fk() != null) {
			bis_qrs.setQrs_ope_no_fk(iary.getQrs_ope_no_fk());
		}
		if (iary.getQrs_ope_ver_fk() != null) {
			bis_qrs.setQrs_ope_ver_fk(iary.getQrs_ope_ver_fk());
		}
		if (iary.getQrs_path_id_fk() != null) {
			bis_qrs.setQrs_path_id_fk(iary.getQrs_path_id_fk());
		}
		if (iary.getQrs_path_ver_fk() != null) {
			bis_qrs.setQrs_path_ver_fk(iary.getQrs_path_ver_fk());
		}
		if (iary.getQrs_time() != null) {
			bis_qrs.setQrs_time(iary.getQrs_time());
		}
		if (iary.getQrs_typ() != null) {
			bis_qrs.setQrs_typ(iary.getQrs_typ());
		}
		if (iary.getSwh_path_id_fk() != null) {
			bis_qrs.setSwh_path_id_fk(iary.getSwh_path_id_fk());
		}
		if (iary.getSwh_path_ver_fk() != null) {
			bis_qrs.setSwh_path_ver_fk(iary.getSwh_path_ver_fk());
		}
		bisQrsDao.save(bis_qrs);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		return outTrx;
	}

	public void deleteFnc() {
		XpbisqrsIA iary = inTrx.getIary().get(0);
		Bis_qrsId id = new Bis_qrsId();
		id.setMdl_id_fk(iary.getMdl_id_fk());
		String mdl_typ_fk = getMdlTypByMdlIDProc(iary.getMdl_id_fk());
		if (mdl_typ_fk == null) {
			outTrx.setRtn_code(E_XPBISQRS_MDL_NOT_FOUND + "");
			outTrx.setRtn_mesg("not found mdl_id[" + iary.getMdl_id_fk() + "]");
			return;
		}
		id.setMdl_typ_fk(mdl_typ_fk);
		id.setOpe_id_fk(iary.getOpe_id_fk());
		id.setOpe_no_fk(iary.getOpe_no_fk());
		id.setOpe_ver_fk(iary.getOpe_ver_fk());
		id.setPath_id_fk(iary.getPath_id_fk());
		id.setPath_ver_fk(iary.getPath_ver_fk());
		id.setQrs_id(iary.getQrs_id());
		id.setTool_id_fk(iary.getTool_id_fk());
		Bis_qrs bis_qrs = bisQrsDao.get(id);
		if (bis_qrs == null) {
			outTrx.setRtn_code(E_XPBISQRS_NOT_FOUND_QRS + "");
			outTrx.setRtn_mesg("not Found Record[BIS_QRS]");
			return;
		}
		bisQrsDao.delete(bis_qrs);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
	}

	public void queryByConditionFnc() {
		/***
		 * 暂支持 By id 查询
		 **/

		List<XpbisqrsOA> oaryList = new ArrayList<XpbisqrsOA>();

		XpbisqrsIA iary = inTrx.getIary().get(0);
		String hql = "FROM Bis_qrs where 1=1 ";
		if (iary.getMdl_id_fk() != null) {
			hql = hql + " and mdl_id_fk='" + iary.getMdl_id_fk() + "'";
		}
		if (iary.getMdl_typ_fk() != null) {
			hql = hql + " and mdl_typ_fk='" + iary.getMdl_typ_fk() + "'";
		}
		if (iary.getOpe_id_fk() != null) {
			hql = hql + " and ope_id_fk='" + iary.getOpe_id_fk() + "'";
		}
		if (iary.getOpe_no_fk() != null) {
			hql = hql + " and ope_no_fk='" + iary.getOpe_no_fk() + "'";
		}
		if (iary.getOpe_ver_fk() != null) {
			hql = hql + " and ope_ver_fk='" + iary.getOpe_ver_fk() + "'";
		}
		if (iary.getPath_id_fk() != null) {
			hql = hql + " and path_id_fk='" + iary.getPath_id_fk() + "'";
		}
		if (iary.getPath_ver_fk() != null) {
			hql = hql + " and path_ver_fk='" + iary.getPath_ver_fk() + "'";
		}
		if (iary.getTool_id_fk() != null) {
			hql = hql + " and tool_id_fk='" + iary.getTool_id_fk() + "'";
		}
		hql = hql + "ORDER BY mdl_typ_fk,mdl_id_fk,path_id_fk,ope_no_fk,ope_id_fk,ope_id_fk,tool_id_fk,qrs_id";
		List<Bis_qrs> bisQrsList = bisQrsDao.find(hql);
		for (Bis_qrs bis_qrs : bisQrsList) {
			XpbisqrsOA oary = new XpbisqrsOA();
			oary.setMdl_id_fk(bis_qrs.getId().getMdl_id_fk());
			oary.setMdl_typ_fk(bis_qrs.getId().getMdl_typ_fk());
			oary.setOpe_id_fk(bis_qrs.getId().getOpe_id_fk());
			oary.setOpe_no_fk(bis_qrs.getId().getOpe_no_fk());
			oary.setOpe_ver_fk(bis_qrs.getId().getOpe_ver_fk());
			oary.setPath_id_fk(bis_qrs.getId().getPath_id_fk());
			oary.setPath_ver_fk(bis_qrs.getId().getPath_ver_fk());
			oary.setTool_id_fk(bis_qrs.getId().getTool_id_fk());

			oary.setQrs_id(bis_qrs.getId().getQrs_id());
			oary.setQrs_ope_id_fk(bis_qrs.getQrs_ope_id_fk());
			oary.setQrs_ope_no_fk(bis_qrs.getQrs_ope_no_fk());
			oary.setQrs_ope_ver_fk(bis_qrs.getQrs_ope_ver_fk());
			oary.setQrs_path_id_fk(bis_qrs.getQrs_path_id_fk());
			oary.setQrs_path_ver_fk(bis_qrs.getQrs_path_ver_fk());
			oary.setQrk_time(bis_qrs.getQrk_time());
			oary.setQrs_time(bis_qrs.getQrs_time());
			oary.setQrs_typ(bis_qrs.getQrs_typ());
			oary.setSwh_path_id_fk(bis_qrs.getSwh_path_id_fk());
			oary.setSwh_path_ver_fk(bis_qrs.getSwh_path_ver_fk());
			oaryList.add(oary);
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
	}

	public void queryQrsKeyListFnc() {

		List<XpbisqrsOA> oaryList = new ArrayList<XpbisqrsOA>();

		XpbisqrsIA iary = inTrx.getIary().get(0);
		String hql = "FROM Bis_qrs where 1=1 ";
		if (iary.getMdl_id_fk() != null) {
			hql = hql + " and mdl_id_fk='" + iary.getMdl_id_fk() + "'";
		}
		if (iary.getMdl_typ_fk() != null) {
			hql = hql + " and mdl_typ_fk='" + iary.getMdl_typ_fk() + "'";
		}
		if (iary.getOpe_id_fk() != null) {
			hql = hql + " and ope_id_fk='" + iary.getOpe_id_fk() + "'";
		}
		if (iary.getOpe_no_fk() != null) {
			hql = hql + " and ope_no_fk='" + iary.getOpe_no_fk() + "'";
		}
		if (iary.getOpe_ver_fk() != null) {
			hql = hql + " and ope_ver_fk='" + iary.getOpe_ver_fk() + "'";
		}
		if (iary.getPath_id_fk() != null) {
			hql = hql + " and path_id_fk='" + iary.getPath_id_fk() + "'";
		}
		if (iary.getPath_ver_fk() != null) {
			hql = hql + " and path_ver_fk='" + iary.getPath_ver_fk() + "'";
		}
		if (iary.getTool_id_fk() != null) {
			hql = hql + " and tool_id_fk='" + iary.getTool_id_fk() + "'";
		}
		hql = hql + "ORDER BY mdl_typ_fk,mdl_id_fk,path_id_fk,ope_no_fk,ope_id_fk,ope_id_fk,tool_id_fk,qrs_id";
		List<Bis_qrs> bisQrsList = bisQrsDao.find(hql);
		String mdl_id_fk = "";
		String mdl_typ_fk = "";
		String ope_id_fk = "";
		String ope_no_fk = "";
		String ope_ver_fk = "";
		String path_id_fk = "";
		String path_ver_fk = "";
		String tool_id_fk = "";
		for (Bis_qrs bis_qrs : bisQrsList) {
			XpbisqrsOA oary = new XpbisqrsOA();
			if (mdl_id_fk.equals(bis_qrs.getId().getMdl_id_fk()) && mdl_typ_fk.equals(bis_qrs.getId().getMdl_typ_fk())
					&& ope_id_fk.equals(bis_qrs.getId().getOpe_id_fk())
					&& ope_no_fk.equals(bis_qrs.getId().getOpe_no_fk())
					&& ope_ver_fk.equals(bis_qrs.getId().getOpe_ver_fk())
					&& path_id_fk.equals(bis_qrs.getId().getPath_id_fk())
					&& path_ver_fk.equals(bis_qrs.getId().getPath_ver_fk())
					&& tool_id_fk.equals(bis_qrs.getId().getTool_id_fk())) {
				continue;
			}
			oary.setMdl_id_fk(bis_qrs.getId().getMdl_id_fk());
			oary.setMdl_typ_fk(bis_qrs.getId().getMdl_typ_fk());
			oary.setOpe_id_fk(bis_qrs.getId().getOpe_id_fk());
			oary.setOpe_no_fk(bis_qrs.getId().getOpe_no_fk());
			oary.setOpe_ver_fk(bis_qrs.getId().getOpe_ver_fk());
			oary.setPath_id_fk(bis_qrs.getId().getPath_id_fk());
			oary.setPath_ver_fk(bis_qrs.getId().getPath_ver_fk());
			oary.setTool_id_fk(bis_qrs.getId().getTool_id_fk());

			oary.setQrs_id(bis_qrs.getId().getQrs_id());
			oary.setQrs_ope_id_fk(bis_qrs.getQrs_ope_id_fk());
			oary.setQrs_ope_no_fk(bis_qrs.getQrs_ope_no_fk());
			oary.setQrs_ope_ver_fk(bis_qrs.getQrs_ope_ver_fk());
			oary.setQrs_path_id_fk(bis_qrs.getQrs_path_id_fk());
			oary.setQrs_path_ver_fk(bis_qrs.getQrs_path_ver_fk());
			oary.setQrk_time(bis_qrs.getQrk_time());
			oary.setQrs_time(bis_qrs.getQrs_time());
			oary.setQrs_typ(bis_qrs.getQrs_typ());
			oary.setSwh_path_id_fk(bis_qrs.getSwh_path_id_fk());
			oary.setSwh_path_ver_fk(bis_qrs.getSwh_path_ver_fk());
			oaryList.add(oary);

			mdl_id_fk = bis_qrs.getId().getMdl_id_fk();
			mdl_typ_fk = bis_qrs.getId().getMdl_typ_fk();
			ope_id_fk = bis_qrs.getId().getOpe_id_fk();
			ope_no_fk = bis_qrs.getId().getOpe_no_fk();
			ope_ver_fk = bis_qrs.getId().getOpe_ver_fk();
			path_id_fk = bis_qrs.getId().getPath_id_fk();
			path_ver_fk = bis_qrs.getId().getPath_ver_fk();
			tool_id_fk = bis_qrs.getId().getTool_id_fk();
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		return;
	}

	public void deleteByConditionFnc() {
		XpbisqrsIA iary = inTrx.getIary().get(0);
		String hql = "FROM Bis_qrs where  mdl_id_fk='" + iary.getMdl_id_fk() + "'";
		hql = hql + " and mdl_typ_fk='" + iary.getMdl_typ_fk() + "'";
		hql = hql + " and ope_id_fk='" + iary.getOpe_id_fk() + "'";
		hql = hql + " and ope_no_fk='" + iary.getOpe_no_fk() + "'";
		hql = hql + " and ope_ver_fk='" + iary.getOpe_ver_fk() + "'";
		hql = hql + " and path_id_fk='" + iary.getPath_id_fk() + "'";
		hql = hql + " and path_ver_fk='" + iary.getPath_ver_fk() + "'";
		hql = hql + " and tool_id_fk='" + iary.getTool_id_fk() + "'";

		List<Bis_qrs> bisQrsList = bisQrsDao.find(hql);
		for (Bis_qrs bis_qrs : bisQrsList) {
			bisQrsDao.delete(bis_qrs);
		}

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		return;
	}

	public void errActionFnc() {
		outTrx.setTrx_id("XPLSTDAT");
		outTrx.setType_id("O");
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}

	private String getMdlTypByMdlIDProc(String mdl_id) {
		String sql = "SELECT MDL_TYP FROM BIS_MDL_DEF WHERE MDL_ID='" + mdl_id + "'";
		List mdlTypeList = bisQrsDao.findBySQL(sql);
		if (mdlTypeList.size() == 0) {
			return null;
		}
		return mdlTypeList.get(0).toString();
	}

}
